package com.example.demo.lock;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

public class TestCountDown {

    public static void main(String[] args) throws InterruptedException {
        testCount();
        System.out.println("------------------");
        testCycli();
    }


    public static void testCount() throws InterruptedException {
        CountDownLatch downLatch = new CountDownLatch(5);

        for (int i = 0; i<5;i++){
            int finalI = i;
            new Thread(()->{
                System.out.println(finalI);
                downLatch.countDown();
            }).start();
        }
        downLatch.await();
        System.out.println("你好");
    }

    public static void testCycli(){
        CyclicBarrier cb = new CyclicBarrier(7,()->{
            System.out.println("召唤神龙");
        });
        for (int i = 0 ;i < 7;i++){
            final int i1 = i;
            new Thread(()->{
                System.out.println(i1);
                try {
                    cb.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

}
